package Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import Interact.Interface;
import Tool.Tool;

public class JudgeBody extends QuestionBody{	
	/** 题面描述 */
	protected String cont;			
	/** 标准答案 */
	protected boolean ans;
	
	public JudgeBody(){
		this(0, null, 0, 0, "", false);
	}
	public JudgeBody(int id, Course course, int grade, int teacherID, String cont, boolean ans){
		super(id, course, grade, teacherID);
		this.cont = cont;
		this.ans = ans;
	}
	
	@Override
	public void toBytes(OutputStream out) throws IOException {
		getIllustration().toBytes(out);
		Tool.writeString(cont, out);
		Tool.writeInt(ans ? 1 : 0, out);
	}
	
	@Override
	public void fromBytes(InputStream in) throws IOException {
		getIllustration().fromBytes(in);
		cont = Tool.readString(in);
		ans = Tool.readInt(in) != 0;
	}
	
	@Override
	public int getType() {
		return Judge.TYPE;
	}
	public String getCont() {
		return cont;
	}
	public void setCont(String cont) {
		this.cont = cont;
	}
	public boolean getAns() {
		return ans;
	}
	public void setAns(boolean ans) {
		this.ans = ans;
	}
	@Override
	public void display(Interface out) throws IOException {
		out.print("<h2>题面</h2><br/><textarea name=\"" + getKey()  + "cont\">在此处输入题目</textarea>");
		out.print("<h2>标准答案</h2><br/><input type=\"checkbox\" name=\"" + getKey() + "answer\" value=1 />");
	}
	@Override
	public void setValueViaInterface(Interface inf) {
		String value = inf.getValue(getKey() + "cont");
		cont = value == null ? "" : value;
		ans = inf.getValues(getKey() + "answer") != null;
	}
}




